package com.csii.mobilebanking.tool;

import android.app.Activity;
import android.content.Context;
import android.text.format.Time;
import java.util.List;
import org.json.*;

import com.csii.mobilebanking.user.CommUser;
import com.csii.mobilebanking.user.GlobalJumpAc;
import com.csii.mobilebanking.user.TResponse;
import com.csii.mobilebanking.user.XGUserAccount;

//针对应用的服务
public class AppService extends JsonService{
	//方法名：获取用户提问和反馈列表
	//Result->Result：TResponse
	//In->AppID：产品标识
	//InOut->aPage：每页记录数
	//Out->list：反馈及回答列表

	//方法名：提交反馈信息(匿名/未登录情况下)
	//Result->Result：TResponse
	//In->AppID：应用编号
	//In->AppVer：应用版本
	//In->DeviceID：设备标识
	//In->Contact：联系方式/姓名/邮件/电话……
	//In->Comment：评论内容
	public static TResponse SubmitFeedback(int AppID, int AppVer, String DeviceID, String Contact, String Comment){
	
		TResponse resp = getResponse();
		try{
			String paramstr="";
			String local_funcid="6201";
			paramstr+=JsonUtils.cxmGetRoJsonStr(local_funcid, "@local_funcid")+",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(JsonService.agentCode, "@agentcode")+",";			
			paramstr+=JsonUtils.cxmGetRoJsonStr(AppID, "@AppID")+",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.AppName, "@AppName")+",";
			if(HpGlobals.mXGUserAccount!=null && HpGlobals.mXGUserAccount.UserID.length()>0){
				paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.UserID, "@userid")+",";	
				paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.Phone, "@username")+",";	
				paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.Phone, "@phone")+",";	
			}else{
				paramstr+=JsonUtils.cxmGetRoJsonStr("", "@username")+",";	
				paramstr+=JsonUtils.cxmGetRoJsonStr("", "@phone")+",";	
			}

			paramstr+=JsonUtils.cxmGetRoJsonStr(Comment, "@Comment");			
			String JsonSendString = BuildSendBody("local_funcid"+local_funcid, paramstr);
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
			if (Root != null){
				String errcode=	Root.getString("@errcode");
				String errdesc=	Root.getString("@errdesc");
				if("0000".equals(errcode)){
					resp.Succed=true;
				}else{
					resp.Succed=false;
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			JsonException(e, resp);
		}
		return resp;
	}

	//方法名：获取系统时间
	//Result->Result：TResponse
	//Out->ServerTime：Time
	public static TResponse GetServerTime(Time ServerTime){
		TResponse resp = getResponse();
		try{
			String paramstr = "";

			String JsonSendString = BuildSendBody("AppService.GetServerTime", paramstr);
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
			if (Root != null){
				JSONObject result = JsonUtils.cxmGetJsonObject(Root, "result");
				if (result != null){
					JsonUtils.cxmJoToObject(resp, JsonUtils.cxmGetJsonObject(result, "Result"));
					if (resp.Succed){
						ServerTime = JsonUtils.cxmJoToTime(result, "ServerTime");
					}
				}
			}
		}catch (Exception e) {
			JsonException(e, resp);
		}
		return resp;
	}

	//方法名：客户端检查版本升级信息
	//Result->Result：TResponse
	//In->AppID：产品标识
	//In->AppVer：产品版本
	//In->Tag：渠道编码
	//Out->UpdateInfo：更新信息
//	public static TResponse CheckUpdate(int AppID, int AppVer, String Tag, AppUpdateInfo UpdateInfo){
//		TResponse resp = getResponse();
//		try{
//			String paramstr ="";
//			String local_funcid ="6002";
//			paramstr+=JsonUtils.cxmGetRoJsonStr(local_funcid, "@local_funcid")+",";
//			paramstr+=JsonUtils.cxmGetRoJsonStr(AppID, "@AppID")+",";
//			paramstr+=JsonUtils.cxmGetRoJsonStr(UpdateInfo.app_name, "@AppName")+",";
//			paramstr+=JsonUtils.cxmGetRoJsonStr(AppVer, "@AppVer");
//			String JsonSendString = BuildSendBody("local_funcid"+local_funcid, paramstr);
//			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
//			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
//			if (Root != null){
//				String errcode=	Root.getString("@errcode");
//				String errdesc=	Root.getString("@errdesc");		
//				if("0000".equals(errcode) || "4007".equals(errcode)){
//					resp.Succed=true;
//				}else{
//					resp.Succed=false;
//				}
//				JSONObject result = JsonUtils.cxmGetJsonObject(Root, "@result");
//				String tempresult=result.toString().replaceAll("@", "");
//				result= JsonUtils.cxmGetJsonObject(tempresult);
//				if (result != null){
//					JsonUtils.cxmJoToObject(UpdateInfo, result);
//				}
//			}
//		}catch (Exception e) {
//			JsonException(e, resp);
//		}
//		return resp;
//	}


	//方法名：用户注册接口
	//Result->Result：TResponse
	//In->UserInfo：注册的用户信息
	public static TResponse Registe(CommUser UserInfo){
		TResponse resp = getResponse();
		try{	
	//		String paramstr = JsonUtils.cxmGetRoJsonStr(UserInfo, "UserInfo");
			String funcid="8501";
			String paramstr0 = JsonUtils.cxmGetRoJsonStr(JsonService.agentCode, "@agent");
			String paramstr1 = JsonUtils.cxmGetRoJsonStr(UserInfo.Phone, "@phone");
			String paramstr2 = JsonUtils.cxmGetRoJsonStr(UserInfo.Password, "@password");
			String paramstr3 = JsonUtils.cxmGetRoJsonStr(funcid, "@funcid");
			String paramstr = paramstr0 + "," +paramstr1+","+paramstr2+","+paramstr3;
			String JsonSendString = BuildSendBody("funcid"+funcid, paramstr);
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
			if (Root != null){
//				JSONObject result = JsonUtils.cxmGetJsonObject(Root, "result");
//				if (result != null){
//					JsonUtils.cxmJoToObject(resp, JsonUtils.cxmGetJsonObject(result, "Result"));
//				}
				String errcode=	Root.getString("@errcode");
				String errdesc=	Root.getString("@errdesc");
				resp.ErrorCode=Integer.parseInt(errcode);
				resp.Memo=errdesc;
				if(errcode.equals("0000")){
					resp.Succed=true;
					UserInfo.UserID=Root.getString("@userid");
				}else{
					resp.Succed=false;

				}
	
			}
		}catch (Exception e) {
			JsonException(e, resp);
		}
		return resp;
	}

	//方法名：登录
	//Result->Result：TResponse
	//In->LoginCode：手机号、电话或者邮箱三者中选其一
	//In->Password：密码
	//Out->XGUserInfo：登录成功之后返回用户信息
	public static TResponse Login(String LoginCode, String Password, XGUserAccount XGUserInfo){
		TResponse resp = getResponse();
		GlobalJumpAc globalfun=new GlobalJumpAc();
		try{
			String funcid="8502";
			String paramstr0 = JsonUtils.cxmGetRoJsonStr(LoginCode, "@logincode");
			String paramstr1 = JsonUtils.cxmGetRoJsonStr(Password, "@password");
			String paramstr2 = JsonUtils.cxmGetRoJsonStr(JsonService.agentCode, "@agent");
			String paramstr3 = JsonUtils.cxmGetRoJsonStr(funcid, "@funcid");
			String paramstr = paramstr0 + "," + 
					paramstr1+","+paramstr2+","+paramstr3;
	
			String JsonSendString = BuildSendBody("funcid"+funcid, paramstr);	
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);

			if (Root != null){
//				JSONObject result = JsonUtils.cxmGetJsonObject(Root, "result");
//				if (result != null){
//					JsonUtils.cxmJoToObject(resp, JsonUtils.cxmGetJsonObject(result, "Result"));
//					if (resp.Succed){
//						JSONObject jsonObject0 = JsonUtils.cxmGetJsonObject(result, "XGUserInfo");
//						if (jsonObject0 != null){
//							JsonUtils.cxmJoToObject(XGUserInfo, jsonObject0);
//						}
//					}
//				}
				String errcode=	Root.getString("@errcode");
				String errdesc=	Root.getString("@errdesc");
				resp.ErrorCode=Integer.parseInt(errcode);
				resp.Memo=errdesc;
				if(errcode.equals("0000")){
					resp.Succed=true;
					XGUserInfo.UserCode=Root.getString("@usercode");
					XGUserInfo.UserID=Root.getString("@userid");
					XGUserInfo.LoginCode=Root.getString("@logincode");
					XGUserInfo.NickName=Root.getString("@nickname");
					XGUserInfo.Email=Root.getString("@email");
					XGUserInfo.Phone=Root.getString("@phone");
					XGUserInfo.IDCard=Root.getString("@idcard");
					XGUserInfo.RealName=Root.getString("@realname");
//					XGUserInfo.UserBalance= globalfun.DoubleM2(Double.parseDouble(Root.getString("@userbalance")));
//					XGUserInfo.UserWinMoney=globalfun.DoubleM2(Double.parseDouble(Root.getString("@userwinmoney")));
//					XGUserInfo.UserEnableMoney=globalfun.DoubleM2(Double.parseDouble(Root.getString("@userbalance"))+Double.parseDouble(Root.getString("@userwinmoney")));
					XGUserInfo.UserLevel=Root.getString("@userlevel");
					XGUserInfo.aBankAccount.BankName=Root.getString("@bankname");
					XGUserInfo.aBankAccount.BankCard=Root.getString("@bankcard");
					XGUserInfo.aBankAccount.BankCity=Root.getString("@bankcity");
					XGUserInfo.aBankAccount.BankType=Root.getString("@banktype");
					
				}else{
					//登录失败处理
					resp.Succed=false;
				}
			}
		}catch (Exception e) {
			JsonException(e, resp);
		}
		return resp;
	}

	//方法名：注销登录
	//Result->Result：TResponse
	public static TResponse Logout(){
		TResponse resp = getResponse();
		try{
			String paramstr = "";
			String local_funcid="6001";
			paramstr += JsonUtils.cxmGetRoJsonStr(local_funcid, "@local_funcid")+",";
			paramstr += JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.UserID, "@userid");
			
			String JsonSendString = BuildSendBody("local_funcid"+local_funcid, paramstr);
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
			if (Root != null){
//				JSONObject result = JsonUtils.cxmGetJsonObject(Root, "result");
//				if (result != null){
//					JsonUtils.cxmJoToObject(resp, JsonUtils.cxmGetJsonObject(result, "Result"));
//				}
				String errcode=	Root.getString("@errcode");
				String errdesc=	Root.getString("@errdesc");
				resp.ErrorCode=Integer.parseInt(errcode);
				resp.Memo=errdesc;
				if(errcode.equals("0000")){
					resp.Succed=true;
				}else{
					resp.Succed=false;
				}
			}
		}catch (Exception e) {
			JsonException(e, resp);
		}
		return resp;
	}
	
	//补填真实姓名和身份证
	public static TResponse RegisteRealname(CommUser UserInfo){
		TResponse resp = getResponse();
		try{	
	//		String paramstr = JsonUtils.cxmGetRoJsonStr(UserInfo, "UserInfo");
			String funcid="8507";
			String updatetype="1";	//0默认 1更新用户身份信息 2更新银行卡信息,5用户绑定手机号码，6用户绑定电子邮箱地址
			String paramstr="";
			paramstr += JsonUtils.cxmGetRoJsonStr(JsonService.agentCode, "@agent")+",";
			paramstr += JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.Phone, "@logincode")+",";
			paramstr += JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.UserID, "@userid")+",";
			paramstr += JsonUtils.cxmGetRoJsonStr(UserInfo.RealName, "@realname")+",";
			paramstr += JsonUtils.cxmGetRoJsonStr(UserInfo.IDCard, "@idcard")+",";
			paramstr += JsonUtils.cxmGetRoJsonStr(funcid, "@funcid")+",";
			paramstr += JsonUtils.cxmGetRoJsonStr(updatetype, "@updatetype");
			String JsonSendString = BuildSendBody("funcid"+funcid, paramstr);
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
			if (Root != null){
//				JSONObject result = JsonUtils.cxmGetJsonObject(Root, "result");
//				if (result != null){
//					JsonUtils.cxmJoToObject(resp, JsonUtils.cxmGetJsonObject(result, "Result"));
//				}
				String errcode=	Root.getString("@errcode");
				String errdesc=	Root.getString("@errdesc");
				resp.ErrorCode=Integer.parseInt(errcode);
				resp.Memo=errdesc;
				if(errcode.equals("0000")){
					resp.Succed=true;
					HpGlobals.mXGUserAccount.RealName=UserInfo.RealName;
					HpGlobals.mXGUserAccount.IDCard=UserInfo.IDCard;
				}else{
					resp.Succed=false;

				}
	
			}
		}catch (Exception e) {
			JsonException(e, resp);
		}
		return resp;
	}
	

	
	//补填银行卡
	public  TResponse FillBankinfo(String bankcity,String banktype,String bankname,String bankcard,String password){
		TResponse resp = getResponse();
		try{	
			String funcid="8507";
			String updatetype="2";
			String paramstr ="";
			paramstr+=JsonUtils.cxmGetRoJsonStr(JsonService.agentCode, "@agent") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(funcid, "@funcid") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.UserID, "@userid") + ",";			
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.LoginCode, "@logincode") + ",";			
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.RealName, "@realname") + ",";	
			paramstr+=JsonUtils.cxmGetRoJsonStr(password, "@password") + ",";			
			paramstr+=JsonUtils.cxmGetRoJsonStr(bankname, "@bankname") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(bankcity, "@bankcity") + ",";			
			paramstr+=JsonUtils.cxmGetRoJsonStr(banktype, "@banktype") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(bankcard, "@bankcard") + ",";	
			paramstr+=JsonUtils.cxmGetRoJsonStr(updatetype, "@updatetype") ;	
			

			String JsonSendString = BuildSendBody("funcid"+funcid, paramstr);
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
			if (Root != null){
				String errcode=	Root.getString("@errcode");
				String errdesc=	Root.getString("@errdesc");
				resp.ErrorCode=Integer.parseInt(errcode);
				resp.Memo=errdesc;
				if(errcode.equals("0000")){
					resp.Succed=true;
					HpGlobals.mXGUserAccount.aBankAccount.BankCard=bankcard;
					HpGlobals.mXGUserAccount.aBankAccount.BankCity=bankcity;
					HpGlobals.mXGUserAccount.aBankAccount.BankName=bankname;
					HpGlobals.mXGUserAccount.aBankAccount.BankType=banktype;					
				}else{
					resp.Succed=false;

				}
	
			}
		}catch (Exception e) {
			JsonException(e, resp);
		}
		return resp;
	}
	
	//提现申请
	public  TResponse MoneyOut(double drawmoney){
		TResponse resp = getResponse();
		try{	
			String funcid="8605";
			String paramstr ="";
			paramstr+=JsonUtils.cxmGetRoJsonStr(JsonService.agentCode, "@agent") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(funcid, "@funcid") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.UserID, "@userid") + ",";			
			paramstr+=JsonUtils.cxmGetRoJsonStr(drawmoney, "@drawmoney") + ",";	
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.aBankAccount.BankName, "@bankname") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.aBankAccount.BankCity, "@bankcity") + ",";			
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.aBankAccount.BankType, "@banktype") + ",";
			paramstr+=JsonUtils.cxmGetRoJsonStr(HpGlobals.mXGUserAccount.aBankAccount.BankCard, "@bankcard");	

			String JsonSendString = BuildSendBody("funcid"+funcid, paramstr);
			String JsonRecvString = JsonUtils.cxm_http_request(JsonSendString, csServerUrl);
			JSONObject Root = JsonUtils.cxmGetJsonObject(JsonRecvString);
			if (Root != null){
				String errcode=	Root.getString("@errcode");
				String errdesc=	Root.getString("@errdesc");
				resp.ErrorCode=Integer.parseInt(errcode);
				resp.Memo=errdesc;
				if(errcode.equals("0000")){
					resp.Succed=true;
				}else{
					resp.Succed=false;

				}
	
			}
		}catch (Exception e) {
			JsonException(e, resp);
		}
		return resp;
	}
	
}
